
// 发起组队请求  {"id":"11"}

// @ts-ignore
export async function execute(client_id,message_json) {

    let login_characters = G.Characters.get_current_login_characters(client_id);

    let target_id = message_json["id"];
    let my_team_data = G.TeamUtils.get_team_data(login_characters["队伍"]);
    if (target_id === login_characters["id"] && my_team_data === undefined) {
        //如果点击的是自己,则只是创建一个队伍
        G.TeamUtils.create_team(login_characters["id"]); //创建队伍
        return;
    }

    if (my_team_data !== undefined) {
        ClientUtils.send_basic_prompt(client_id, "你已经在队伍中");
        return;
    }

    let target_characters = _CharactersData.get(target_id);
    let target_team_data = G.TeamUtils.get_team_data(target_characters["队伍"]);
    //对方有队伍
    if (target_team_data["成员"].length >= 5) {
        ClientUtils.send_basic_prompt(client_id, "对方队伍已满员！");
        return;
    }
    let my_lelvel = login_characters["等级"];
    if (my_lelvel < target_team_data["限制等级"][0] || my_lelvel > target_team_data["限制等级"][1]) {
        ClientUtils.send_basic_prompt(client_id, "不满足对方队伍的等级要求！");
        return;
    }
    if (target_team_data["申请"] > 5) {
        ClientUtils.send_basic_prompt(client_id, "对方申请名单已满，请过稍后再试");
        return;
    }
    let apply_flag = true;
    //校验是否已经申请过
    for (let index = 0; index < target_team_data["申请"].length; index++) {
        let apply = target_team_data["申请"][index];
        if (apply["id"] === login_characters["id"]) {
            apply_flag = false;
            ClientUtils.send_basic_prompt(client_id, "你已经申请过了,请等待队长审核");
            break;
        }
    }
    //提交入队申请
    if (apply_flag) {
        let data = G.TeamUtils.build_team_characters_data(login_characters);
        target_team_data["申请"].push(data);
        ClientUtils.send_basic_prompt(client_id, "入队申请提交成功，请耐心等候~");
        ClientUtils.send_characters_basic_prompt(target_team_data["id"], `玩家 ${login_characters["名称"]} 申请加入你的队伍，请尽快处理！`);
    }

}